package com.example.whereismystuff.views;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;

import com.example.whereismystuff.R;
import com.example.whereismystuff.classes.*;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.AssetManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

/**
 * Sets up the sign up view and instantiates all of the buttons and fields required
 * @author NullPointers
 *
 */
public class SignUp extends Activity {
	private Button createAccount;
	private EditText e_name, e_lastName, e_username, e_email, e_password, e_retypePassword;
	private String name, lastName, username, email, password, retypePassword;
	
	private AlertDialog.Builder alertBoxPassword;
	private AlertDialog.Builder alertBoxNull;
	
	private UserDataManager userManager;
	
	private LoginDataBaseAdapter loginDataBaseAdapter;
	
	private Intent i;
	private final int MIN_STRING_LENGTH = 0;
	private final String LOCKED = "false";
	
	/**
	 * Method used to Instantiate Object and set the Layout for the Activity
	 * @param Bundle
	 */
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.sign_up);
		
		loginDataBaseAdapter=new LoginDataBaseAdapter(this);
		loginDataBaseAdapter=loginDataBaseAdapter.open();
		
		createAccount = (Button) findViewById(R.id.createUser);
		e_name = (EditText) findViewById(R.id.firstName);
		e_lastName = (EditText) findViewById(R.id.lastName);
		e_username = (EditText) findViewById(R.id.newUsername);
		e_email = (EditText) findViewById(R.id.emailInput);
		e_password = (EditText) findViewById(R.id.newPassword);
		e_retypePassword = (EditText) findViewById(R.id.retypePassword);
		alertBoxPassword = new AlertDialog.Builder(this);
		alertBoxNull = new AlertDialog.Builder(this);
		
	}
	/**
	 * Handles what happens when the button is pressed.
	 * @param v
	 * @throws IOException 
	 */
	public void onCreateAccount(View v) throws IOException{
		name = e_name.getText().toString();
		lastName = e_lastName.getText().toString();
		username = e_username.getText().toString();
		email = e_email.getText().toString();
		password = e_password.getText().toString();
		retypePassword = e_retypePassword.getText().toString();
		
		if(username.equals("")||password.equals("")||retypePassword.equals(""))
		{
			alertBoxNull.setMessage("Please Fill In All Fields");
			alertBoxNull.setNeutralButton("Ok", new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
				}
			});
			alertBoxNull.show();
			return;
		}
		// check if both password matches
		if(!password.equals(retypePassword))
		{
			alertBoxPassword.setMessage("Passwords do not match");
			alertBoxPassword.setNeutralButton("Ok", new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
				}
			});
			alertBoxPassword.show();
			return;
		}
		else
		{
		    // Save the Data in Database
			loginDataBaseAdapter.insertEntry(username, password, "false", "false");
		    Toast.makeText(getApplicationContext(), "Account Successfully Created ", Toast.LENGTH_LONG).show();
		    try{
				Class c = Class.forName("com.example.whereismystuff.views.DashboardView");
				Bundle extras = new Bundle();
				extras.putString("Username", username);
				i = new Intent(getApplicationContext(), c);
				i.putExtras(extras);
				startActivity(i);
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			}
		}
		
	}
	
	/**
	 * On resume method
	 */
	protected void onResume(){
		super.onResume();
	}
}
